<?php
/**
 * QQ SDK callback
 */
//QQ默认里面已经配置了session 这里做了修改
require_once(dirname(__FILE__)."/../../includes/class.qq.api/qqConnectAPI.php");
$qc = new QC();

$access_tonken=$qc->qq_callback();//返回的token
$qq_openid=$qc->get_openid();//用户的ID

//全局变量
$IN_defaultpl="default";
include(dirname(__FILE__)."/../../index.inc.php");//首页配置
//判断是否绑定了用户
$sql="SELECT u.`uid`, u.`uname`, u.`realname`, u.`ncname`, u.`password`, u.`addtime`, u.`lastlogintime`, u.`logintime`,u.`login_num`,u.`status`,t.`id` AS user_token_id FROM `".$tbprefix."user` AS u INNER JOIN `".$tbprefix."user_token` AS t ON t.`uid`=u.`uid` WHERE t.`qq_openid`='{$qq_openid}' AND t.`type_form`='1' AND u.`is_valid`='1' AND 1";
$rs=$db->get_one($sql);
 //判断是否已经绑定用户信息 已经绑定直接登录
 if(!empty($rs['uid'])){
 	if($rs['status']==1){
 		//login success save login message
 		$dataArray['`logintime`']=$m_now_time;				//登录时间
 		$dataArray['`lastlogintime`']=$rs['logintime'];		//最后登录时间
 		$dataArray['`login_num`']=$rs['login_num']+1; 		//登录次数
 		$dataArray['`modify_ip`']=$fun->get_web_ip(); 		//登录IP
 		$db->update("`".$tbprefix."user`",$dataArray,$condition=" `uid`='".$rs['uid']."' AND 1");
 		//update qq access_token time 这里是QQ绑定 授权时间更新 tonken
 		$dataArr['changetime']=$m_now_time;
 		$dataArr['access_token']=$access_tonken;
 		$db->update("`".$tbprefix."user_token`",$dataArr,$condition=" `id`='".$rs['user_token_id']."'  AND 1");
 		
 		//存储登录的基本信息
 		@$_SESSION['web_user_uid']=$fun->authcode($rs['uid'],'ENCODE',$pwdconstant,$expiry=0);
 		 
 		//同步登陆
				
		/* shopnc login */
		
		$shoplogin = SHOPNC_DOMAIN."index.php?act=login&user_name={$login_user}&password={$login_password}&form_submit=ok";
		echo '<iframe height=0 width=0 style="display:none;" src="'.$shoplogin.'"></iframe>';
		
		/* shopnc login ok*/
		
		/* discuz login */
		
		$discuzlogin = DISCUZ_DOMAIN."member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&username={$login_user}&password={$login_password}";
		echo '<iframe height=0 width=0 style="display:none;" src="'.$discuzlogin.'"></iframe>';
		
		/* discuz login */
 		 
 		//来自的连接 用于三网合并 1首页 2论坛 3商城
 		$url_from=isset($_REQUEST["from"])?$_REQUEST["from"]:"1";
 		$backurl = $fun->back_to_which_from($url_from);
 		//$fun->msg("登陆成功,正在前往首页...",$backurl,3,$title="登录提示",$back_color="f78a12",$link_url="../../");
 		echo "<script  type='text/javascript' language='javascript'>window.location.href='{$backurl}';</script>";
 		exit();
 	}else{
		$fun->msg("您的用户名出现异常,我们已经对您的用户名做出冻结处理,您无法登录","./../../".$list_url['login'],2,$title="登录提示",$back_color="f78a12",$link_url="./../../");
	}
   }else{
     	if(empty($access_tonken)){
     		$fun->msg("QQ授权注册失败,正在跳转到网站注册","./../../".$list_url['register'],2,$title="注册提示",$back_color="f78a12",$link_url="./../../");
     	}else{
     		//授权成功 跳转到QQ授权绑定页面 以下做授权access_token更新操作
     		$qq_token_sql="SELECT t.id,t.`uid` FROM  `".$tbprefix."user_token` AS t WHERE t.`qq_openid`='{$qq_openid}' AND t.`type_form`='1' AND 1";
     		$qq_rs=$db->get_one($qq_token_sql);
     		if($qq_rs['id']){
     			//存在绑定授权 更新
     			//update qq access_token time 这里是QQ绑定 授权时间更新
     			$dataArr['changetime']=$m_now_time;
 	 			$dataArr['access_token']=$access_tonken;
     			$dataArr['type_form']=1;
     			$db->update("`".$tbprefix."user_token`",$dataArr,$condition=" `id`='{$qq_rs['id']}' AND 1");
     			$qq_token_id=$qq_rs['id'];
     		}else{
     			//不存在授权 新增一条
     			//保存授权信息
     			$dataArr['access_token']=$access_tonken;
     			$dataArr['qq_openid']=$qq_openid;
     			$dataArr['addtime']=$m_now_time;
     			$dataArr['type_form']=1;
     			$db->insert($table="`".$tbprefix."user_token`",$dataArr);
     			$qq_token_id=$db->insert_id();
     		}
     		
     		//存储保存的token表主键 用于绑定
     		@$_SESSION['web_qq_token_id']=$fun->authcode($qq_token_id,'ENCODE',$pwdconstant,$expiry=0);
     		
     		//跳转到QQ绑定
     		echo "<script  type='text/javascript' language='javascript'>window.location.href='./../../".$list_url['bindqqcallback']."';</script>";
     		exit();
     	}
   }

?>
